package strcy.cn.market.controller;


import jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import strcy.cn.market.service.Impl.MailService;
import strcy.cn.market.utils.Result;

import java.util.Map;

@RestController
public class EmailController {
    @Autowired
    private MailService mailService;
    /**
     * 发送验证码给指定的邮箱
     * @param requestBody email-->指定的邮箱
     * @return
     */
    @PostMapping("/sendCode")
    public Result sendCode(@RequestBody Map<String ,String> requestBody) throws MessagingException {
        String email = requestBody.get("email");
        if (email == null||email.isEmpty()) {
            return Result.error("email参数不能为空");
        }
        mailService.sendVerificationCode(email);
        return Result.success("验证码已发送");
    }

    /**
     * 验证验证码
     * @param requestBody email-->指定的邮箱  code-->验证码
     * @return
     */
    @PostMapping("/verifyCode")
    public Result verifyCode(@RequestBody Map<String ,String> requestBody) {
        String email = requestBody.get("email");
        String code = requestBody.get("code");
        if (email == null || code == null) {
            return Result.error("error");
        }
        boolean isValid = mailService.verifyCode(email, code);
        return Result.success(isValid ? "验证码正确" : "验证码错误或已过期");
    }

}
